using UnityEngine;

public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
	[Header("Singleton")]
	[SerializeField]
	private bool dontDestroyOnLoad;

	private static T instance;

	public static T Instance
	{
		get
		{
			if (instance == null)
			{
				instance = Object.FindObjectOfType<T>();
			}
			return instance;
		}
	}

	protected virtual void Awake()
	{
		if (instance != null && instance != this)
		{
			Object.Destroy(this);
			return;
		}
		instance = GetComponent<T>();
		if (dontDestroyOnLoad)
		{
			Object.DontDestroyOnLoad(base.gameObject);
		}
	}
}
